unit USFAddVarForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TfmAddVar = class(TForm)
    Label1: TLabel;
    fmAddVar: TLabel;
    cbClass: TComboBox;
    edValue: TEdit;
    Button1: TButton;
    btnOK: TButton;
    Label2: TLabel;
    edName: TEdit;
    procedure edValueChange(Sender: TObject);
    procedure edValueKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
    function VerifyInput: boolean;
  end;

var
  fmAddVar: TfmAddVar;

implementation

{$R *.dfm}

procedure TfmAddVar.edValueChange(Sender: TObject);
begin
  btnOK.Enabled := (edName.Text <> '') and VerifyInput;
end;

procedure TfmAddVar.edValueKeyPress(Sender: TObject; var Key: Char);
begin
  if Ord(Key) in [VK_DELETE,VK_BACK] then exit;
end;

function TfmAddVar.VerifyInput: boolean;

  function InArray(c: char; x: array of char): boolean;
  var i: integer;
  begin
    Result := false;
    for i := Low(x) to High(x) do
      if (c = x[i]) then begin
        Result := true;
        break;
      end;
  end;

  function Forbid(s: string; chrs: array of char): boolean;
  var i: integer;
  begin
    Result := false;
    for i := 1 to Length(s) do
      if not InArray(s[i], chrs) then begin
        Result := true;
        break;
      end;
  end;

begin
  case cbClass.ItemIndex of
    0,
    1,
    3: Result := (edValue.Text <> '') and not Forbid(edValue.Text, ['0','1','2','3','4','5','6','7','8','9']);
    2,
    5: Result := (edValue.Text <> '') and not Forbid(edValue.Text, ['-','0','1','2','3','4','5','6','7','8','9']);
    4: Result := (edValue.Text <> '') and not Forbid(edValue.Text, ['-','.',DecimalSeparator,'0','1','2','3','4','5','6','7','8','9']);
    else Result := (edValue.Text <> '');
  end;
end;

end.
